package liumengbaio.lolzhangyoubao.Activity.Fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;

import liumengbaio.lolzhangyoubao.Activity.Adapter.HeroSoundAdapter;
import liumengbaio.lolzhangyoubao.Activity.InFo.FreeHeroInfo;
import liumengbaio.lolzhangyoubao.Activity.InFo.HeroSoundInfo;
import liumengbaio.lolzhangyoubao.R;

/**
 * Created by lanouhn on 16/9/6.
 */
public class FragmentSound extends Fragment implements View.OnClickListener {
    private HeroSoundInfo heroSoundInfo = new HeroSoundInfo();
    private HeroSoundAdapter heroSoundAdapter;
    private ListView soundListView;

    private Button btn_music, btn_play;


   String userid;

    public static FragmentSound newInstants(FreeHeroInfo info) {
        FragmentSound fragmentSound = new FragmentSound();
        Bundle bundle = new Bundle();
        bundle.putSerializable("soundinfo",info);
        fragmentSound.setArguments(bundle);
        return fragmentSound;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_sound, null);
        return view;

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        FreeHeroInfo soundinfo = (FreeHeroInfo) getArguments().getSerializable("soundinfo");
        userid=soundinfo.getId();
        initView(getView());
        initData();
        getDatafromWeb();
    }

    private void initView(View view) {
        soundListView = (ListView) view.findViewById(R.id.sound_listview);
        btn_music = (Button) view.findViewById(R.id.btn_music);
        btn_play = (Button) view.findViewById(R.id.btn_play);
    }

    private void initData() {

    }

    private void getDatafromWeb() {
        String url = "http://lol.zhangyoubao.com/apis/rest/RolesService/ring?roleid="+userid+"&page=1&i_=EsgMWh3USt5BgC/cGtS1hKY9wRoLT+mJ6iTQiBqMiVY=&deviceId=EsgMWh3USt5BgC/cGtS1hKY9wRoLT+mJ6iTQiBqMiVY=&t_=1473249040708&p_=4469&v_=400901&a_=lol&pkg_=com.anzogame.lol&d_=android&osv_=18&cha=ucMartket&u_=&modle_=Coolpad%205311&api=RolesService.ring&time=1473249040&game=lol&secretId=AKIDz8krbsJ5yKBZQpn74WFkmLPx3gnPhESA&secretVersion=v1.0&nonce=361279&secretSignature=IX1YBGfJFy6LSGEPQbHVl+2EXQo=";
        OkHttpClient client = new OkHttpClient();
        //创建一个Request，传入请求地址
        Request request = new Request.Builder()
                .url(url)
                .build();
        //执行请求并返回一个Call对象
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                handler.sendEmptyMessage(0);
            }

            @Override
            public void onResponse(Response response) throws IOException {
                //获取请求结果
                String result = "";
                result = response.body().string();
                //解析数据
                Gson gson = new Gson();
                heroSoundInfo = gson.fromJson(result, HeroSoundInfo.class);
                if (heroSoundInfo.getData().size() > 0) {
                    handler.sendEmptyMessage(1);
                } else {
                    handler.sendEmptyMessage(0);
                }

            }
        });
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Toast.makeText(getActivity(), "网络请求失败!", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    heroSoundAdapter = new HeroSoundAdapter(heroSoundInfo, getActivity());
                    soundListView.setAdapter(heroSoundAdapter);
                    break;


            }
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_play:
                btn_music.setVisibility(View.VISIBLE);
                break;
            case R.id.btn_music:
                break;
        }
    }


}
